package im.yangqiang.android.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.InputFilter;
import android.text.InputType;
import android.util.AttributeSet;
import android.widget.EditText;

import im.yangqiang.android.ui.R;


/**
 * 密码控件
 * Created by Carlton on 16/8/30.
 */

public class PasswordEditText extends EditText
{
    /**
     * 密码长度
     */
    private int mPasswordLength = 1;
    /**
     * 分割线大小
     */
    private int mDividingLineWidth = 5;
    /**
     * 分割线颜色
     */
    private int mDividingLineColor = Color.BLACK;
    /**
     * 输入的长度
     */
    private int mInputLength = 0;
    /**
     * 密码圆点半径
     */
    private int mPasswordRadius = 40;
    private RectF mPasswordRect = new RectF();

    public PasswordEditText(Context context)
    {
        super(context);
        init(context, null, 0);
    }

    public PasswordEditText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public PasswordEditText(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);

    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr)
    {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PasswordEditText);
        mPasswordLength = typedArray.getInt(R.styleable.PasswordEditText_passwordLength, 1);
        mDividingLineWidth = typedArray.getDimensionPixelSize(R.styleable.PasswordEditText_dividerLineSize, 5);
        mDividingLineColor = typedArray.getColor(R.styleable.PasswordEditText_dividerLineColor, Color.BLACK);
        mPasswordRadius = typedArray.getDimensionPixelSize(R.styleable.PasswordEditText_passwordRadius, 40);
        typedArray.recycle();

        mPasswordPaint.setAntiAlias(true);
        mRectPaint.setAntiAlias(true);
        setMaxLines(1);
        InputFilter[] filters = {new InputFilter.LengthFilter(mPasswordLength)};
        setFilters(filters);
        setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
        setCursorVisible(false);

        if (isInEditMode())
        {
            setText(String.valueOf(1));
        }
    }

    private Paint mPasswordPaint = new Paint();
    private Paint mRectPaint = new Paint();

    @Override
    protected void onDraw(Canvas canvas)
    {
        // 绘制边框
        final int width = getWidth();
        int rectSpace = width / mPasswordLength;
        mRectPaint.setColor(mDividingLineColor);
        mRectPaint.setStrokeWidth(mDividingLineWidth);
        for (int i = 1; i < mPasswordLength; ++i)
        {
            int startX = rectSpace * i;
            canvas.drawLine(startX, 0, startX, getHeight(), mRectPaint);
        }
        // 绘制密码
        mPasswordPaint.setColor(Color.BLACK);
        final int ovalWidth = mPasswordRadius;
        for (int i = 0; i < mInputLength; ++i)
        {
            int startX = rectSpace * i;
            int left = startX + (rectSpace - ovalWidth) / 2;
            int top = (getHeight() - ovalWidth) / 2;
            mPasswordRect.set(left, top, left + ovalWidth, top + ovalWidth);
            canvas.drawOval(mPasswordRect, mPasswordPaint);
        }
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter)
    {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        mInputLength = text.length();
        if (mOnPasswordFinishListener != null && mInputLength == mPasswordLength)
        {
            mOnPasswordFinishListener.onPasswordFinish(text);
        }
    }

    private OnPasswordFinishListener mOnPasswordFinishListener;

    public void setOnPasswordFinishListener(OnPasswordFinishListener listener)
    {
        mOnPasswordFinishListener = listener;
    }

    public interface OnPasswordFinishListener
    {
        void onPasswordFinish(CharSequence text);
    }
}
